<template>
	<view class="box">
		<view class="tab">
			<view class="tab-item tab-ac">Hi，欢迎来到家谱管理</view>
		</view>
		<view class="form">
			<view class="form-item">
				<view class="form-item-label">手机号码</view>
				<view class="form-item-input">
					<input type="number" placeholder="请输入手机号" placeholder-class="form-item-input-ac" v-model="form.phone">
				</view>
			</view>
			<view class="form-item">
				<view class="form-item-label">验证码</view>
				<view class="form-item-input">
					<input type="number" placeholder="请输入验证码" placeholder-class="form-item-input-ac" v-model="form.sms_code">
					<view class="qrCode" @click="getCode()">{{code}}</view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-item-label">密码</view>
				<view class="form-item-input">
					<input type="password" placeholder="请输入密码" placeholder-class="form-item-input-ac" v-model="form.password">
			
				</view>
			</view>
		</view>
		<view class="btns flex-center" @click="loginIn">
			<image :src="mixiImgUrl + '/static/anniu.png'" mode=""></image>
			<view class="flex-center">注册</view>
		</view>
		<view class="footer-tx">
			已有账号？  <text @click="toLogin">去登陆</text>
		</view>
		<btnlogin :zheshow='zheshow' ref="btnlogin" @submit="submit" />
	</view>
</template>

<script>
	import {smsHttp,loginHttp} from '@/common/http/api.js'
	import btnlogin from '@/components/login/butlogin.vue';
	export default {
		components: {
			btnlogin
		},
		data(){
			return {
				form:{
					code: '',
					phone: '',
					sms_code: '',
					password: ''
				},
				code: '获取验证码',
			}
		},
		methods:{
			getCode(){
				
				if(this.code === '获取验证码') {
					if(this.form.phone === '') {
						uni.showToast({
							title: '请输入手机号',
							icon: 'none'
						})
						return
					}
					uni.showLoading()
					smsHttp({phone: this.form.phone}).then(res => {
						uni.hideLoading()
						this.form.sms_code = ''
						let time = 60
						let timer = setInterval(()=>{
							if(time != 0) {
								time --
								this.code = time + 's'
							} else {
								this.code = '获取验证码'
								clearInterval(timer)
							}
						},1000)
					})
				}
			},
			toLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			async loginIn() {
				await this.$mValidate(
					[{
						require: true,
						key: 'phone',
						message: '请输入手机号'
					}, {
						require: true,
						key: 'sms_code',
						message: '请输入验证码'
					}, {
						require: true,
						key: 'password',
						message: '请输入密码'
					}], this.form)
					this.$refs.btnlogin.show()
			},
			submit(userInfo){
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						console.log(loginRes);
						let data = {
							...this.form,
							code: loginRes.code,
							type: 1,
							userInfo
						}
						uni.showLoading()
						loginHttp(data).then(res => {
							// uni.setStorageSync("token", res.data.token)
							// uni.setStorageSync("userInfo", res.data.userInfo)
							// uni.reLaunch({
							// 	url: '/pages/index/index'
							// })
							uni.hideLoading()
							uni.showToast({
								title: '注册成功',
								icon: 'none'
							})
							setTimeout(()=>{
								uni.navigateBack()
							},1500)
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F6F1E7;
	}
	.tab {
		padding: 0 84rpx;
		padding-top: 70rpx;
		width: 100%;
		box-sizing: border-box;
		
		display: flex;
		align-items: center;
		.tab-item {
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 40rpx;
			color: #333333;
		}
		.tab-line {
			width: 3rpx;
			height: 41rpx;
			background: #333333;
			border-radius: 2rpx;
			margin: 0 46rpx;
		}
		.tab-ac {
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 40rpx;
			color: #84643B;
		}
	}
	.form {
		
		width: 100%;
		box-sizing: border-box;
		padding: 0 84rpx;
		padding-top: 115rpx;
		.form-item {
			width: 100%;
			margin-bottom: 70rpx;
			.form-item-label {
				font-family: Source Han Sans CN;
				font-weight: 500;
				font-size: 32rpx;
				color: #333333;
				margin-bottom: 70rpx;
			}
			.form-item-input {
				width: 100%;
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #E5DCC9;
				display: flex;
				align-items: center;
				input {
					flex: 1;
					font-family: Source Han Sans CN;
					font-weight: 400;
					font-size: 30rpx;
					color: #000;
				}
				.form-item-input-ac {
					font-family: Source Han Sans CN;
					font-weight: 400;
					font-size: 30rpx;
					color: #BBBBBB;
				}
				.qrCode {
					font-family: Source Han Sans CN;
					font-weight: 400;
					font-size: 30rpx;
					color: #84643B;
					margin-left: 20rpx;
				}
			}
		}
		.form-item:last-child {
			margin-bottom: 0;
		}
	}
	.footer {
		width: 100%;
		box-sizing: border-box;
		padding: 0 84rpx;
		padding-top: 31rpx;
		.footer-l {
			font-family: Source Han Sans CN;
			font-weight: 400;
			font-size: 24rpx;
			color: #999999;
			text {
				color:#84643B;
			}
		}
		.footer-r {
			font-family: Source Han Sans CN;
			font-weight: 400;
			font-size: 24rpx;
			color: #999999;
		}
	}
	.btns {
		width: 100%;
		box-sizing: border-box;
		padding: 0 84rpx;
		margin-top: 142rpx;
		position: relative;
		height: 109rpx;
		view {
			width: 100%;
			height: 100%;
			font-weight: 400;
			font-size: 32rpx;
			color: #FFFFFF;
			position: absolute;
			top: 0;
			left: 0;
		}
	}
	.footer-tx {
		width: 100%;
		padding-top: 24rpx;
		text-align: center;
		font-family: Source Han Sans CN;
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
		text {
			color: #84643B;
		}
	}
	.box {
		padding-bottom: 100rpx;
	}
</style>